from pynput import keyboard
from overlay_window import OverlayWindow
from settings_window import SettingsWindow
import time
import threading
import tkinter as tk


class MouseFinder:
    def __init__(self):
        # 创建主窗口
        self.root = tk.Tk()
        self.root.withdraw()  # 隐藏主窗口

        # 创建效果实例
        self.overlay_window = OverlayWindow(self.root)

        # 创建设置窗口
        self.settings = SettingsWindow(self.root)

        # 跟踪上一次按键时间
        self.last_press_time = 0
        self.double_press_delay = 0.3

        # 创建键盘监听器（在单独的线程中运行）
        self.keyboard_thread = threading.Thread(target=self.start_keyboard_listener)
        self.keyboard_thread.daemon = True
        self.keyboard_thread.start()

    def start_keyboard_listener(self):
        """在单独的线程中运行键盘监听器"""
        with keyboard.Listener(on_press=self.on_press) as listener:
            listener.join()

    def on_press(self, key):
        try:
            if key == keyboard.Key.ctrl_l:
                current_time = time.time()
                if current_time - self.last_press_time < self.double_press_delay:
                    # 使用 after 方法在主线程中执行 GUI 操作
                    self.root.after(0, self.show_effect)
                self.last_press_time = current_time
        except AttributeError:
            pass

    def show_effect(self):
        """显示白色圆圈效果"""
        self.root.after(0, self.overlay_window.show)

    def run(self):
        # 显示设置窗口
        self.settings.show()
        # 运行主循环
        self.root.mainloop()


if __name__ == "__main__":
    finder = MouseFinder()
    finder.run()